package com.bestcem.xm.ticket.controller.v2;

import com.bestcem.xm.common.core.constant.RegTypes;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.common.core.enums.ResourceErrorCodeEnum;
import com.bestcem.xm.ticket.controller.convert.TicketConfigConvert;
import com.bestcem.xm.ticket.controller.param.SimpleUpdateTicketConfigParam;
import com.bestcem.xm.ticket.controller.vo.SimpleTicketConfigVO;
import com.bestcem.xm.ticket.dao.TicketConfigDao;
import com.bestcem.xm.ticket.entity.mongo.TicketConfig;
import com.bestcem.xm.ticket.enums.TicketConfigStateEnum;
import com.bestcem.xm.ticket.service.TicketConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * v2/工单配置列表
 *
 * @author zq.ding <zq.ding@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/31 10:58
 */
@Api(tags = "v2/工单配置列表", value = "/ticket/v2/ticketconfigList")
@RestController(value = "TicketV2TicketConfigListController")
@RequestMapping(value = "/ticket/v2/ticketconfigList", produces = MediaType.APPLICATION_JSON_VALUE)
public class TicketConfigListController {
    @Resource
    private TicketConfigService ticketConfigService;
    @Resource
    private TicketConfigDao ticketConfigDao;

    /**
     * ticketConfigs 查询简单列表(id, title, state)
     *
     * @param id 问卷项目id
     * @return ticketConfig 缩略信息列表
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/8/16 14:23
     */
    @ApiOperation(value = "ticketConfig 简单列表查询(id, title, state)")
    @GetMapping(value = "/")
    public XmResultJson<List<SimpleTicketConfigVO>> getTicketConfigList(@ApiParam(value = "问卷项目id", required = true)
                                                                        @RequestParam(value = "project_id") String id) {
        if (!Pattern.matches(RegTypes.OBJECT_ID, id)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT, "问卷项目id格式错误");
        }
        List<TicketConfig> ticketConfigList = ticketConfigDao.listBySurveyId(id, true);
        return XmResultJson.success(TicketConfigConvert.entityToSimpleTicketConfigVOList(ticketConfigList));
    }

    /**
     * ticketConfigs 简单更新(id, title, state)
     *
     * @param id    工单规则id
     * @param param 更新参数(标题/状态)
     * @return 更新的工单规则id
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/8/16 14:23
     */
    @ApiOperation(value = "ticketConfig 简单更新(id, title, state)")
    @PutMapping(value = "/{id}")
    public XmResultJson<String> putTicketConfigList(@PathVariable(value = "id") String id,
                                                    @ApiParam(value = "工单规则更新参数", required = true)
                                                    @RequestBody SimpleUpdateTicketConfigParam param) {
        if (!Pattern.matches(RegTypes.OBJECT_ID, id)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT, "工单规则id格式错误");
        }
        TicketConfig ticketConfig = ticketConfigDao.findById(id);
        if (Objects.isNull(ticketConfig)) {
            return XmResultJson.fail(ResourceErrorCodeEnum.NOT_FOUND, "工单规则不存在");
        }
        // 更新标题
        if (!ObjectUtils.isEmpty(param.getTitle())) {
            ServiceResult<Object> serviceResult = ticketConfigService.updateTicketConfigTitle(id, param.getTitle());
            if (!serviceResult.isSuccess()) {
                return XmResultJson.fail(serviceResult.getErrorData(), serviceResult.getMsg());
            }
        }
        // 更新状态
        if (!ObjectUtils.isEmpty(param.getState())) {
            TicketConfigStateEnum stateEnum = TicketConfigStateEnum.getByIndex(param.getState());
            if (Objects.isNull(stateEnum)) {
                return XmResultJson.fail(ResourceErrorCodeEnum.NOT_FOUND, "规则状态不存在");
            }
            ServiceResult<Object> serviceResult = ticketConfigService.updateTicketConfigState(id, stateEnum);
            if (!serviceResult.isSuccess()) {
                return XmResultJson.fail(serviceResult.getErrorData(), serviceResult.getMsg());
            }
        }
        return XmResultJson.success(id);
    }
}
